package org.example.controller.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.controller.BookController;
import org.example.domain.Book;
import org.example.domain.PageBean;
import org.example.domain.response.ResponseResult;
import org.example.services.impl.BookServiceImpl;
import org.example.utils.PageConvertPageBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Type;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookControllerImpl implements BookController {

    @Autowired
    private BookServiceImpl bookService;

//    查询所有书籍
//    @Override
//    @GetMapping
//    @PreAuthorize("hasAuthority('book:selectAll')")
//    public ResponseResult getAllBook(){
//        List<Book> allBook = bookService.getAllBook();
//        return ResponseResult.success(allBook);
//    }

//    通过id删除书籍
    @Override
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('book:delete')")
    public ResponseResult DeleteBook(@PathVariable @NotNull(message = "书籍ID不能为null")
                                                 @Min(value = 0,message = "书籍ID不能小于0") Integer id){
        return bookService.deleteBook(id);
    }
//    添加书籍
    @Override
    @PostMapping
    @PreAuthorize("hasAuthority('book:save')")
    public ResponseResult SaveBook(@RequestBody Book book){
        return bookService.saveBook(book);
    }

//    修改书籍
    @Override
    @PutMapping
    @PreAuthorize("hasAuthority('book:upate')")
    public ResponseResult UpdateBook(@RequestBody @Validated(Book.Update.class) Book book){
        return bookService.updateBook(book);
    }

//    通过id查询
    @Override
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('book:selectOne')")
    public ResponseResult getBook(@PathVariable Integer id){
        return bookService.getBook(id);
    }

//    通过type查询
    @Override
    @GetMapping("/2/{type}/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('book:selectAll')")
    public ResponseResult getTypeByPage(@PathVariable String type,@PathVariable int pageSize,@PathVariable int currentPage){
        return bookService.getTypeByPage(type,pageSize,currentPage);
    }


    //    通过name查询
    @Override
    @GetMapping("/1/{name}/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('role:selectAll')")
    public ResponseResult getNameByPage(@PathVariable String name,@PathVariable int pageSize,@PathVariable int currentPage) {
        return bookService.getNameByPage(name,pageSize,currentPage);
    }



//    分页
    @Override
    @GetMapping("/3/{pageSize}/{currentPage}")
    public ResponseResult getPage(@PathVariable int pageSize, @PathVariable int currentPage){
        return bookService.getPage(currentPage, pageSize);
    }

    @Override
    @PostMapping("/cover/{bookId}")
    @PreAuthorize("hasAuthority('book:update')")
    public ResponseResult uploadBookCover(@RequestParam("file") MultipartFile file,@PathVariable int bookId) {
        return bookService.uploadBookCover(file,bookId);
    }

    @Override
    @GetMapping("/show")
    public ResponseResult getAllSlideShow() {
        return bookService.getAllSlideShow();
    }

    @Override
    @PostMapping("/show/{bookId}")
    @PreAuthorize("hasAuthority('slideShow:save')")
    public ResponseResult saveSlideShow(@RequestParam("file") MultipartFile file,@PathVariable int bookId) {
        return bookService.saveSlideShow(file,bookId);
    }

    @Override
    @DeleteMapping("/show/{slideShowId}")
    @PreAuthorize("hasAuthority('slideShow:delete')")
    public ResponseResult deleteSlideShow(@PathVariable int slideShowId) {
        return bookService.deleteSlideShow(slideShowId);
    }
}
